package com.education.welco.controller;


import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.Convert;
import com.education.welco.pojo.JrSchool;
import com.education.welco.pojo.STheoryClassroom;
import com.education.welco.pojo.TClass;
import com.education.welco.pojo.TEquipment;
import com.education.welco.service.ISTheoryClassroomService;
import com.education.welco.service.ITClassService;
import com.education.welco.service.ITEquipmentService;
import com.education.welco.service.ITSchoolService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 设备 信息操作处理
 *
 * @author d
 * @date 2022-06-20
 */
@RestController
@RequestMapping("/tEquipment")
public class TEquipmentController extends BaseController {

    @Autowired
    private ITEquipmentService tEquipmentService;

    @Autowired
    private ITSchoolService schoolService;

    @Autowired
    private ITClassService classService;

    @Autowired
    private ISTheoryClassroomService sTheoryClassroomService;

    /**
     * 管理端
     * 查询设备列表
     */
    @GetMapping("/list")
    public ApiReturnObject list(TEquipment tEquipment, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        //根据目录id查询
        List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
        PageInfo pageInfo = new PageInfo<>(list);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }

    /**
     * 教务端设备列表查询   (查回分配给学校的设备)
     * 设备名称equName、授权班级classroomId、授权状态isSqclass、学校id-schoolId
     *
     * @param tEquipment
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/listByEdu")
    public ApiReturnObject listByEdu(TEquipment tEquipment, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        //根据目录id查询
        List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
        PageInfo pageInfo = new PageInfo<>(list);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }

    /**
     * 教务端班级下拉列表
     *
     * @param schoolId
     * @return
     */
    @GetMapping("/classlistByEdu")
    public ApiReturnObject classlistByEdu(Integer schoolId) {
        //根据学校id查询班级列表
        List<TClass> tClasses = classService.selectTClassAllBySchoolId1(schoolId);
        //PageInfo pageInfo = new PageInfo<>(tClasses);
        return new ApiReturnObject("200", "查询成功", tClasses);
    }

    /**
     * 教务端设备解绑接口
     * 如果当前班级正在上课拒绝解绑
     *
     * @param tEquipment
     * @return
     */
    @GetMapping("/equUnbind")
    public ApiReturnObject equUnbind(TEquipment tEquipment) {
        TEquipment tEquipment1 = tEquipmentService.selectTEquipmentById(tEquipment.getId());
        //如果当前班级正在上课拒绝解绑
        List<STheoryClassroom> sTheoryClassrooms = sTheoryClassroomService.selectSTheoryClassroomListForNowLive(tEquipment1.getClassroomId(), new Date());
        if (sTheoryClassrooms.size() != 0) {
            return new ApiReturnObject("200", "当前班级正在直播上课中,无法解绑！", "当前班级正在直播上课中,无法解绑！");
        }
        //解绑设备，清空班级星信息，该授权状态
        tEquipment.setClassroomId(0);
        tEquipment.setClassroomName(" ");
        tEquipment.setIsSqclass("0");
        tEquipmentService.updateTEquipment(tEquipment);
        return new ApiReturnObject("200", "班级解绑成功!", "");
    }

    /**
     * 教务端设备绑定接口
     *
     * @param tEquipment
     * @return
     */
    @PostMapping("/teaBind")
    public ApiReturnObject teaBind(@RequestBody TEquipment tEquipment) {
        //查询班级信息
        TClass tClass = classService.selectTClassById(tEquipment.getClassroomId());
        tEquipment.setClassroomName(tClass.getClassName());
        tEquipment.setIsSqclass("1");
        tEquipmentService.updateTEquipment(tEquipment);
        return new ApiReturnObject("200", "班级授权成功!", "");
    }

    /**
     * 管理端操作-新增保存设备
     *
     * @param tEquipment
     * @return
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody TEquipment tEquipment) {
        //校验设备号是否存在
        TEquipment tEquipmentParam = new TEquipment();
        tEquipmentParam.setEquCode(tEquipment.getEquCode());
        if (tEquipmentService.selectTEquipmentList(tEquipmentParam).size() > 0) {
            return new ApiReturnObject("201", "设备序列号已存在", "");
        }
        tEquipment.setCreateTime(new Date());
        //是否涉及授权
        if (null != tEquipment.getSchoolId()) {
            //查询学校信息
            JrSchool jrSchool = schoolService.selectTSchoolByIdForjrschool(tEquipment.getSchoolId());
            tEquipment.setSchoolName(jrSchool.getSchoolName());
            tEquipment.setIsSqschool("1");
            tEquipmentService.insertTEquipment(tEquipment);
            return new ApiReturnObject("200", "添加成功", "");
        }
        tEquipmentService.insertTEquipment(tEquipment);
        return new ApiReturnObject("200", "添加成功", "");
    }

    /**
     * 详情设备
     */
    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id) {
        TEquipment result = tEquipmentService.selectTEquipmentById(id);
        return new ApiReturnObject("200", "查询成功", result);
    }

    /**
     * 管理端的操作修改接口
     * 修改保存设备
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody TEquipment tEquipment) {
        System.out.println(tEquipment);
        //1.编辑2.授权接口
        if (null != tEquipment.getSchoolId()) {
            //查询学校信息
            JrSchool jrSchool = schoolService.selectTSchoolByIdForjrschool(tEquipment.getSchoolId());
            tEquipment.setSchoolName(jrSchool.getSchoolName());
            tEquipment.setIsSqschool("1");
            tEquipmentService.updateTEquipment(tEquipment);
            return new ApiReturnObject("200", "授权成功!", "");
        }
        tEquipmentService.updateTEquipment(tEquipment);
        return new ApiReturnObject("200", "修改成功", "");
    }

    /**
     * 管理端
     * 删除设备   需要判断设备是否满足删除条件(0
     */
    @PostMapping("/del")
    public ApiReturnObject remove(@RequestBody Map map) {
        String ids = map.get("ids").toString();
        List<String> stringIds = Arrays.asList(Convert.toStrArray(ids));
        //判断是否设备是否绑定班级(根据设备id查找设备)
        for (String stringId : stringIds) {
            TEquipment tEquipment = tEquipmentService.selectTEquipmentById(Integer.valueOf(stringId));
            if (null == tEquipment.getClassroomId() || tEquipment.getClassroomId().equals(0)) {
                tEquipmentService.deleteTEquipmentById(stringId);
            } else {
                return new ApiReturnObject("200", "设备被分配给班级，禁止删除!", "");
            }
        }
        return new ApiReturnObject("200", "删除成功", "");
    }


    /**
     * 查找班级下是否绑定了设备决定是否可以开启直播 班级id
     * /tEquipment/queryEquStateForClass?classId=
     *
     * @param classId
     * @return
     */
    @GetMapping("/queryEquStateForClass")
    public ApiReturnObject classequip(Integer classId) {
        TEquipment tEquipment = new TEquipment();
        tEquipment.setClassroomId(classId);
        List<TEquipment> tEquipments = tEquipmentService.selectTEquipmentList(tEquipment);
        if (null != tEquipments && tEquipments.size() > 0) {
            return new ApiReturnObject("200", "查询成功", 1);
        }
        return new ApiReturnObject("200", "查询成功", 0);
    }

}